import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin, loadEnv } from 'electron-vite'
import { PluginOption } from 'vite'
import { setupVitePlugin } from './renderer-vite-plugin-config/renderer.vite.plugin.config'
import pkg from './package.json'
import dayjs from 'dayjs'

export default defineConfig(_env => {
  const root = process.cwd()
  // 读取.env中配置的环境变量
  const env = loadEnv(_env.mode, root)
  console.log('执行命令: ', _env.command, ', mode:', _env.mode, ', env配置文件数据: ', env)
  const vitePluginArr: (PluginOption | PluginOption[])[] = []
  const { dependencies, devDependencies, name, version } = pkg
  const __APP_INFO__ = {
    pkg: { dependencies, devDependencies, name, version },
    lastBuildTime: dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')
  }
  setupVitePlugin(_env.command, _env.mode, vitePluginArr, env, version)
  return {
    main: {
      plugins: [externalizeDepsPlugin()]
    },
    preload: {
      plugins: [externalizeDepsPlugin()]
    },
    renderer: {
      css: {
        preprocessorOptions: {
          scss: {
            additionalData: `@use '@assets/scss/_functions.scss' as *;
  @use '@assets/scss/_mixins.scss' as *;`
          }
        }
      },
      resolve: {
        alias: {
          '@renderer': resolve('src/renderer/src'),
          '@stores': resolve('src/renderer/src/stores'),
          '@assets': resolve('src/renderer/src/assets'),
          '@utils': resolve('src/renderer/src/utils')
        }
      },
      plugins: vitePluginArr,
      define: {
        __APP_INFO__: JSON.stringify(__APP_INFO__)
      }
    }
  }
})
